package routers

import (
	"context"
	"fmt"
	"github.com/go-chi/chi/v5"
	"github.com/go-chi/chi/v5/middleware"
	"go.uber.org/zap"
	"gsc-datasets/internal/conf"
	dao "gsc-datasets/internal/db/dao"
	pgdb "gsc-datasets/internal/db/sqlc"
	upcos "gsc-datasets/pkg/upstream/scf/cos"
	upsms "gsc-datasets/pkg/upstream/scf/sms"
)

type RestRouter struct {
	slog *zap.SugaredLogger
	dao  *dao.PgDao
	sms  *conf.SmsSection
	cos  *conf.CosSection
	r    chi.Router
}

// New initialization
func New(
	slog *zap.SugaredLogger,
	dao *dao.PgDao,
	sms *conf.SmsSection,
	cos *conf.CosSection,
) *RestRouter {
	return &RestRouter{
		slog: slog,
		dao:  dao,
		sms:  sms,
		cos:  cos,
		r:    chi.NewRouter(),
	}
}

func (s *RestRouter) ChiRouter() chi.Router {
	return s.r
}

func (s *RestRouter) Logger(module string) *zap.SugaredLogger {
	return s.slog.With("module", module)
}

func (s *RestRouter) Dao() *pgdb.Queries {
	return s.dao.Q
}

func (s *RestRouter) TxDao() *dao.TxRunner {
	return s.dao.NewTxRunner()
}

func (s *RestRouter) SmsClient() *upsms.SmsClient {
	return upsms.NewClient(s.sms.ScfEndPoint)
}

func (s *RestRouter) CosClient() *upcos.CosClient {
	return upcos.NewClient(s.cos.ScfEndPoint)
}

func (s *RestRouter) GetSmsAppID(mockID string) string {
	return fmt.Sprintf("%s@%s", mockID, s.sms.AppID)
}

func (s *RestRouter) GetSmsSignName() string {
	return s.sms.SignName
}

func (s *RestRouter) GetReqID(ctx context.Context) string {
	return middleware.GetReqID(ctx)
}

func (s *RestRouter) GetCosBucketPrefix() string {
	return s.cos.BucketPrefix
}

func (s *RestRouter) GetCosBucketCustomCDNDomain() string {
	return s.cos.BucketCustomCDNDomain
}
